<template>
	<view class="page">
		<view class=" title" @tap="choice">
			选择类型
		</view>
		<view class="cueentobj" v-if="current=='添加医院'">
			<view class="tit_yiyuan">
				{{current}}
			</view>
			<view class="grid">
				<view class="name">
					医院名称：
				</view>
				<view class="input">
					<input type="text" value="" v-model="hsopital.name" placeholder="请输入医院名称" />
				</view>
			</view>
			<view class="grid">
				<view class="name">
					医院电话：
				</view>
				<view class="input">
					<input type="text" value="" v-model="hsopital.phone" placeholder="请输入医院电话" />
				</view>
			</view>
		</view>
		<view class="cueentobj" v-else-if="current=='添加科室'">
			<view class="tit_yiyuan">
				{{current}}
			</view>
			<view class="grid">
				<view class="name">
					请选择医院：
				</view>
				<view class="input">
					<xfl-select :list="list" :clearable="false" :showItemNum="5" :listShow="false" :isCanInput="true"
						:style_Container="'height: 80rpx; font-size: 16px;width:100%'" :placeholder="'请选择医院'"
						:focusShowList='"showList1Hospilal"' @input='manualSerachHospital()' :selectHideType="'hideAll'"
						@change='hospitalEven()'>
					</xfl-select>
				</view>
			</view>
			<view class="grid">
				<view class="name">
					请输入科室名称：
				</view>
				<view class="input">
					<input type="text" value="" v-model="department.name" placeholder="请输入科室名称" />
				</view>
			</view>
			<view class="grid">
				<view class="name">
					请绑定工程师：
				</view>
				<view class="input">
					<xfl-select :list="list1" :clearable="false" :showItemNum="5" :listShow="false" :isCanInput="true"
						:style_Container="'height: 80rpx; font-size: 16px;width:100%'" :placeholder="'请选择工程师'"
						:focusShowList='"showList1Hospilal"' @input='manualSerachEngineer()' :selectHideType="'hideAll'"
						@change='engineerEven()'>
					</xfl-select>
				</view>
			</view>
		</view>
		<view class="cueentobj" v-else-if="current=='添加设备'">
			<view class="tit_yiyuan">
				{{current}}
			</view>

			<view class="grid">
				<view class="name">
					请选择医院：
				</view>
				<view class="input">
					<xfl-select :list="list" :clearable="false" :showItemNum="5" :listShow="false" :isCanInput="true"
						:style_Container="'height: 80rpx; font-size: 16px;width:100%'" :placeholder="'请选择医院'"
						:focusShowList='"showList1Hospilal"' @input='manualSerachHospital()' :selectHideType="'hideAll'"
						@change='hospitalEven()'>
					</xfl-select>
				</view>
			</view>
			<view class="grid">
				<view class="name">
					请选择科室：
				</view>
				<view class="input">
					<xfl-select :list="list2" :clearable="false" :showItemNum="5" :listShow="false" :isCanInput="true"
						:focusShowList='"showList2Hospilal"'
						:style_Container="'height: 80rpx; font-size: 16px;width:100%'" :placeholder="'请选择科室'"
						@change='departmentEven()' @input='manualSerachDepartment()'>
					</xfl-select>
				</view>
			</view>
			<view class="grid">
				<view class="name">
					请输入设备名称：
				</view>
				<view class="input">
					<input type="text" value="" v-model="equipment.name" placeholder="请输入设备名称" />
				</view>
			</view>
			<view class="grid">
				<view class="name">
					请输入设备型号：
				</view>
				<view class="input">
					<input type="text" value="" v-model="equipment.marking" placeholder="请输入设备型号" />
				</view>
			</view>
			<view class="grid">
				<view class="name">
					请输入设备序列号：
				</view>
				<view class="input">
					<input type="text" value="" v-model="equipment.serial" placeholder="请输入设备序列号" />
				</view>
			</view>
			<view class="grid">
				<view class="name">
					软件版本：
				</view>
				<view class="input">
					<input type="text" value="" v-model="equipment.software_version" placeholder="请输入软件版本" />
				</view>
			</view>
			<view class="oppo">
				<view class="name">
					附件说明（选填）：
				</view>
				<view class="input">
					<textarea type="text" v-model="equipment.enclosure" placeholder="请填写..." />
				</view>
			</view>


		</view>
		<view class="submit" @tap="addnow">
			立即添加
		</view>
	</view>
</template>

<script>
	import xflSelect from '../../components/xfl-select/xfl-select.vue';
	export default {
		components: {
			xflSelect //注册为子组件
		},
		data() {
			return {
				current: '',
				hospital_name: '',
				equipment: {
					name: '',
					marking: '',
					serial: '',
					enclosure: '',
					software_version: '',
					hospitalid: null,
					department_id: null
				},
				hsopital: {
					name: '',
					phone: ''
				}, //医院新增
				department: {
					name: '',
					hospitalid: '',
					engineer_id: ''
				}, //科室新增
				list: [], //医院列表用于展示
				hospital_list: [], //医院列表存数据
				list1: [], //工程师列表展示
				engineer_list: [], //工程师列表存数据	
				list2: [], //科室展示
				departmentList: [], //科室列表存数据

			}
		},
		onLoad() {},
		watch: {
			current(n, o) {
				if (n == '添加科室') {
					this.list = []
					this.hospital_list = []
					this.hospital_name = ''
					this.getHospital()
					this.engineerlist()
				} else if (n == '添加设备') {
					this.list = []
					this.hospital_list = []
					this.hospital_name = ''
					this.getHospital()
				}
			},
			'equipment.hospitalid': function(n, o) {
				if (this.current == '添加设备') {
					this.list2 = []
					this.departmentList = []
					this.getDepartment()
				}
			},
			/* hospital_name(n,o){
				if(this.current=='添加设备'&&this.hospital_name!=''){
					this.list2=[]
					this.departmentList=[]
					this.equipment.hospitalid=null
					this.getDepartment()
				}
			} */
		},
		methods: {
			/* 选择医院 */
			hospitalEven(e) {
				if (this.current == '添加科室') {
					this.department.hospitalid = this.hospital_list.find(item => item.name == e.orignItem).id

				} else {

					this.equipment.hospitalid = this.hospital_list.find(item => item.name == e.orignItem).id
				}
			},
			/* 选择工程师 */
			engineerEven(e) {
				this.department.engineer_id = this.engineer_list.find(item => item.nickname == e.orignItem).id
			},
			/* 选择科室 */
			departmentEven(e) {
				this.equipment.department_id = this.departmentList.find(item => item.name == e.orignItem).id
			},
			/* 工程师列表 */
			engineerlist() {
				this.$https.post('/hospital/getengineer', {
					nickname: this.nickname
				}).then(res => {
					this.engineer_list = res.data
					this.list1 = []
					res.data.forEach(item => {
						this.list1.push(item.nickname)
					})
				})
			},
			/* 手动搜索工程师 */
			manualSerachEngineer(e) {
				if (e.detail.value == '')
					this.nickname = e.detail.value
				this.engineerlist()
			},
			/* 手动搜索医院 */
			manualSerachHospital(e) {
				this.hospital_name = e.detail.value
				this.getHospital()
			},
			/* 手动搜索科室 */
			manualSerachDepartment(e) {
				this.equipment.department_name = e.detail.value
				this.getDepartment()
			},
			/* 获取医院列表 */
			getHospital() {
				this.$https.post('/hospital/search', {
					name: this.hospital_name
				}).then(res => {
					this.hospital_list = res.data
					this.list = []
					res.data.forEach(item => {
						this.list.push(item.name)
					})
				})
			},
			/* 获取科室列表 */
			getDepartment() {
				this.$https.post('/phase/getDepartment', {
					hospital_id: this.equipment.hospitalid,
					name: this.equipment.department_name
				}).then(res => {
					this.list2 = []
					this.departmentList = res.data
					res.data.forEach(item => {
						this.list2.push(item.name)
					})
				})
			},
			/*  */
			choice() {
				uni.showActionSheet({
					itemList: ['添加医院', '添加科室', '添加设备'],
					success: res => {
						if (res.tapIndex == 0) {
							this.current = '添加医院'
						} else if (res.tapIndex == 1) {
							this.current = '添加科室'
						} else if (res.tapIndex == 2) {
							this.current = '添加设备'
						}
					},
					fail: function(res) {
						uni.showToast({
							title: '选择失败，请重试！',
							icon: 'none',
						})
					}
				});
			},
			addnow() {
				switch (this.current) {
					case '添加医院':
						if (!(/^1[3456789]\d{9}$/.test(this.hsopital.phone))) {
							uni.showToast({
								title: '手机号码格式错误，请重试！',
								icon: 'none'
							})
							return false;
						}
						this.$https.post('/hospital/add', {
							name: this.hsopital.name,
							mobile: this.hsopital.phone,
						}).then(res => {
							if (res.msg == '成功') {
								uni.showToast({
									title: '添加医院成功！',
									icon: 'none'
								})
							} else {
								uni.showToast({
									title: '添加医院失败，请重试！',
									icon: 'none'
								})
							}
						})
						break
					case '添加科室':
						this.$https.post('/hospital/addDepar', {
							name: this.department.name,
							hospital_id: this.department.hospitalid,
							user_id: this.department.engineer_id
						}).then(res => {
							if (res.msg == '成功') {
								uni.showToast({
									title: '添加科室成功！',
									icon: 'none'
								})
							} else {
								uni.showToast({
									title: '添加科室失败，请重试！',
									icon: 'none'
								})
							}
						})
						break
					case '添加设备':
						let enclosure_data = null
						if (this.equipment.enclosure) {
							enclosure_data = 0
						} else {
							enclosure_data = 1
						}
						console.log(this.equipment.name)
						console.log(enclosure_data)
						console.log(this.equipment.department_id)
						console.log(this.equipment.enclosure)
						console.log(this.equipment.hospitalid)
						console.log(this.equipment.marking)
						console.log(this.equipment.serial)
						console.log(this.equipment.software_version)
						this.$https.post('/hospital/addEquip', {
							name: this.equipment.name,
							enclosure_data: enclosure_data,
							department_id: this.equipment.department_id,
							enclosure: this.equipment.enclosure,
							hospital_id: this.equipment.hospitalid,
							marking: this.equipment.marking,
							serial: this.equipment.serial,
							software_version: this.equipment.software_version,
						}).then(res => {
							console.log(res)
							if (res.msg == '成功') {
								uni.showToast({
									title: '添加设备成功！',
									icon: 'none'
								})
								setTimeout(() => {
									uni.reLaunch({
										url: './createOrder'
									})
								}, 1000)
							} else {
								uni.showToast({
									title: '添加设备失败，请重试！',
									icon: 'none'
								})
							}
						})
						break
				}
			}


		}
	}
</script>

<style lang="scss" scoped>
	textarea {
		border-bottom: 2rpx solid #3B3D3D;
		width: 100%;
		margin-top: 10px;
		min-height: 100rpx;
	}

	.submit {
		width: 622rpx;
		height: 80rpx;
		border-radius: 80rpx;
		background-color: $public-oclor;
		line-height: 80rpx;
		text-align: center;
		color: #FFFFFF;
		margin: 30rpx auto;
		position: fixed;
		bottom: 0px;
		left: 50%;
		transform: translateX(-50%);
	}


	input {
		border-bottom: 2rpx solid #3B3D3D;
		height: 80rpx;
	}

	.oppo {
		background-color: none;
		align-items: center;
		color: #3B3D3D;
		font-weight: 500;
		background-color: #fff;
		font-size: 28rpx;
		height: 80rpx;
		padding: 10rpx;
	}

	.grid {
		padding: 10rpx;
		display: grid;
		grid-template-columns: 270rpx 1fr;
		background-color: none;
		align-items: center;
		color: #3B3D3D;
		font-weight: 500;
		background-color: #fff;
		font-size: 28rpx;
		height: 80rpx;

	}

	.tit_yiyuan {
		padding: 20rpx 32rpx;
		background: linear-gradient(91deg, #e2ecfb 0%, rgba(255, 255, 255, 0) 100%);
		font-size: 28rpx;
		font-weight: 600;
		color: #6AA6FA;
		line-height: 33rpx;
	}

	.page {
		box-sizing: border-box;

	}

	.title {
		margin: 32rpx;
		padding: 20rpx 32rpx;
		display: inline-block;
		/* 		background: linear-gradient(86deg, rgba(226, 140, 129, 0.15) 0%, rgba(226, 140, 129, 0) 100%); */
		font-size: 28rpx;
		font-weight: 600;
		background-color: #1A66FF;
		color: #fff;
		border-radius: 10rpx;
		line-height: 33rpx;
	}
</style>
